home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 14642 / 14642.xpi / chrome / content / historyPiechart.js < prev    next >
Text File  |  2009-09-22  |  9KB  |  224 lines

  1. /* Copyright 2009, Boomtango.com.  All Rights Reserved. */
  2. /* historyPiechart.js
  3.  * Responsible for piechart view
  4.  */
  5.  
  6.  
  7. bthistory.controllers["piechart"] = {
  8.     handleUpArrow: function(){
  9.     },
  10.     handleDownArrow: function(){
  11.     },
  12.     onHistoryAdd: function(data) {
  13.     },
  14.     onHistoryChange: function(data) {
  15.     },
  16.     handleResize: function(){
  17.     },
  18.     queryTracker: function(){
  19.         return bthistory.storage.querySummary(
  20.             bthistory._range.start,
  21.             bthistory._range.end
  22.         );
  23.     },
  24.     loadView: function(){
  25.         document.loadOverlay("chrome://boomtango/content/historyPiechart.xul", this);
  26.     },
  27.     observe: function(subject, topic, data) {
  28.         if(topic == "xul-overlay-merged"){
  29.             bthistory.app.log("historyPiechart::overlayLoaded");
  30.             this.loadMergedView();
  31.         }
  32.     },
  33.     /*
  34.         loadMergedView is handled after view has been merged.  
  35.     */
  36.     loadMergedView: function() {
  37.         bthistory.app.log("historyPiechart::loadMergedView");
  38.  
  39.         var data = bthistory._data;
  40.         var body = document.getElementById("body");
  41.         if(data.types.length == 0){
  42.             var label= document.createElement("label");
  43.             label.setAttribute("value", bthistory.app.getString("history.nodatafound"));
  44.             label.className = "nodatafound";
  45.             body.appendChild(label);
  46.             document.getElementById("piebox").setAttribute("hidden", "true");
  47.         } else {
  48.             this.loadPiechart(data.types);
  49.  
  50.             var types = bthistory.tracker.types;
  51.             for(var x in types){
  52.                 if(data.urls.hasOwnProperty(x) && 
  53.                    bthistory.app.getTrackerEnabled(x)){
  54.                     this.loadTopSites(x, data.urls[x]);
  55.                 }
  56.             }
  57.          }
  58.  
  59.         document.getElementById("btfilter_deck").setAttribute("hidden", "true");
  60.         document.getElementById("bubble_back").setAttribute("hidden", "true");
  61.  
  62.     },
  63.     loadTopSites: function(type, data){
  64.         if(data && data.length){
  65.             var name = bthistory.app.tracker.types[type].name_plural;
  66.             var len = data.length;
  67.             var container = document.getElementById("urlsummary");
  68.             var box = document.createElement("vbox");
  69.             box.className = "topurlbox";
  70.             box.setAttribute("style", "border-color: " +
  71.                     bthistory.app.getTrackerColor(type) + ";");
  72.  
  73.             var title = bthistory.app.getString("pie.type.topurls", name);
  74.             var label = document.createElement("label");
  75.             label.className = "topurl_title";
  76.             label.setAttribute("value", title);
  77.             box.appendChild(label);
  78.             //box.setAttribute("flex", "1");
  79.  
  80.             var fi = Components.classes["@mozilla.org/browser/favicon-service;1"].getService(Components.interfaces.nsIFaviconService);
  81.             var io = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService2);
  82.             for(var x = 0; x < data.length; x++){
  83.                 var item = data[x];
  84.                 var hbox = document.createElement('hbox');
  85.                 hbox.setAttribute("flex", "1");
  86.                 hbox.setAttribute("style", "background-color: transparent;");
  87.                 hbox.setAttribute("contentID", item.ftsrowid);
  88.                 var uri = io.newURI(item.url, null, null); 
  89.                 var iconURI = fi.getFaviconImageForPage(uri);
  90.                 var img = document.createElement("image");
  91.                 img.setAttribute("src", iconURI.spec);
  92.                 img.className = "calendaricon";
  93.                 img.setAttribute("style", "background-color: transparent;");
  94.                 var title = item.title || item.url;
  95.                 var vbox = document.createElement('vbox');
  96.                 vbox.setAttribute("style", "background-color: transparent;");
  97.                 var linkNode = document.createElement("label");
  98.                 linkNode.className = "historyItem";
  99.                 linkNode.setAttribute("value", title);
  100.                 linkNode.setAttribute("crop", "end");
  101.                 linkNode.setAttribute("flex", "1");
  102.                 vbox.appendChild(img);
  103.                 hbox.appendChild(vbox);
  104.                 hbox.appendChild(linkNode);
  105.  
  106.                 label = document.createElement("label");
  107.                 label.className = "topurl";
  108.                 if(item.timespent){
  109.                     label.setAttribute("value", bthistory.duration(item.timespent));
  110.                 }
  111.                 hbox.appendChild(label);
  112.                 box.appendChild(hbox);
  113.             }
  114.             
  115.             container.appendChild(box);
  116.         }
  117.     },
  118.     loadPiechart: function(data){
  119.         bthistory.app.log("piechart::loadPiechart");
  120.         const width = 200;
  121.         const height = 200;
  122.         const center = width / 2;
  123.         const radius = center;
  124.         var canvas = document.getElementById('pie');
  125.         canvas.style.width = width + "px";
  126.         canvas.style.height = height + "px";
  127.         canvas.width = width;
  128.         canvas.height = height;
  129.  
  130.         var len = data.length;
  131.         var total = 0;
  132.         var other = 0;
  133.         var webindex = -1;
  134.         for(var x = 0; x < len; x++){
  135.             if(data[x].type == 'web'){
  136.                 webindex = x;
  137.                 total = data[x].timespent;
  138.             } else if(bthistory.app.getTrackerEnabled(data[x].type)){
  139.                 other += data[x].timespent;
  140.             }
  141.         }
  142.  
  143.         data[webindex].timespent =  total - other;
  144.         var curr = 0;
  145.         var ctx = canvas.getContext('2d');
  146.         for(var x = 0; x < len; x++){
  147.             var val = data[x].timespent / total;
  148.             ctx.beginPath();
  149.             ctx.moveTo(center, center);
  150.             ctx.arc(  
  151.                 center,
  152.                 center,
  153.                 radius,
  154.                 Math.PI * (-0.5 + 2 * curr),
  155.                 Math.PI * (-0.5 + 2 * (curr + val)),
  156.                 false
  157.             );
  158.             ctx.lineTo(center, center);
  159.             ctx.closePath();
  160.             ctx.fillStyle = bthistory.app.getTrackerColor(data[x].type);
  161.             ctx.fill();
  162.             ctx.restore();
  163.  
  164.             curr += val;
  165.         }
  166.  
  167.         // load text
  168.         var label = document.createElement('label');
  169.         label.setAttribute('value', bthistory.duration(total) + " (100%)");
  170.         label.className = "typeSummaryTop";
  171.         var titlebox = document.getElementById("typesummary_title");
  172.         var databox = document.getElementById("typesummary_data");
  173.         databox.appendChild(label);
  174.         if(len >  1){
  175.                 
  176.             data.sort(function(a,b) { return b.timespent - a.timespent;});
  177.             
  178.             var func = function(type, timespent){
  179.                 var hbox = document.createElement("hbox");
  180.                 var label = document.createElement('label');
  181.                 var name;
  182.                 if(type == 'web'){
  183.                     name = bthistory.app.getString("pie.type.other");
  184.                 } else if (bthistory.app.tracker.types[type]){
  185.                     name = bthistory.app.tracker.types[type].name_plural;
  186.                 } else {
  187.                     name = bthistory.app.getString("tracker.unknown");
  188.                 }
  189.                 label.setAttribute('value', bthistory.app.getString("pie.type.timespent", name));
  190.                 label.className = "typeSummary";
  191.                 label.setAttribute("style", "color: " + bthistory.app.getTrackerColor(type) + ";");
  192.  
  193.                 hbox.appendChild(label);
  194.                 titlebox.appendChild(label);
  195.  
  196.                 label = document.createElement('label');
  197.                 label.className = "typeSummary";
  198.                 label.setAttribute("value", 
  199.                         bthistory.duration(timespent) + " (" + Math.floor(timespent * 100 / total) +  "%)");
  200.                 databox.appendChild(label);
  201.             };
  202.             var otherspent = 0;
  203.             for(var x = 0; x < len; x++){
  204.                 var type = data[x].type;
  205.                 var timespent = data[x].timespent;
  206.                 if(type == "web"){
  207.                     otherspent = timespent;
  208.                 } else if(bthistory.app.getTrackerEnabled(type)){
  209.                     func(type, timespent);
  210.                 }
  211.             }
  212.             func('web', otherspent);
  213.         }
  214.     },
  215.     
  216.     QueryInterface: function(iid) {  
  217.         if (iid.equals(this.ci.nsIObserver) ||  
  218.                iid.equals(this.ci.nsISupports)) {  
  219.             return this;  
  220.         } 
  221.         throw Components.result.NS_ERROR_NO_INTERFACE;  
  222.     }
  223. };
  224.